// OSCClass iOSC(iPhone App) test sketch
// OSCClass version 1.0.1 (Arduino ver0014)
// Copyright (c) recotana(http://recotana.com).  All right reserved.

/*
iOSC is OSCsend Application for iPhone
http://recotana.com/iphone/iosc/

iOSC setting
            OSCMessage    type  value on  off
button1    /ard/ledyel    int          1   0   :info-AlternateMode "ON" =toggle sw
button2    /ard/ledgrn    int          1   0
button3    /ard/ledred    int          1   0
slider1    /ard/ledpwm    int  min:0 max:255

host setting IP address 192.168.0.99 , port 10000

*/

#include "Ethernet.h"
#include "OSCClass.h"


#define  LED_RED 2
#define  LED_GRN 3
#define  LED_YEL 4
#define  LED_PWM 6

  OSCMessage recMes;
  
  OSCClass osc(&recMes);

  byte serverMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  byte serverIp[]  = { 192, 168, 0, 99 };
  int  serverPort  = 10000;
  
//  byte gateway[]   = { 192, 168, 0, 1 };
//  byte subnet[]    = { 255, 255, 255, 0 };

  
  char *topAddress="/ard";
  char *subAddress[4]={ "/ledred" , "/ledgrn" , "/ledyel" , "/ledpwm"};
  

  
void setup() {
  
       //for message logging
       
//     Serial.begin(19200);
  
  
      Ethernet.begin(serverMac ,serverIp);
 //    Ethernet.begin(serverMac ,serverIp ,gateway ,subnet);
    
     //setting osc recieve server
     osc.begin(serverPort);
     
     pinMode(LED_RED, OUTPUT); 
     pinMode(LED_GRN, OUTPUT); 
     pinMode(LED_YEL, OUTPUT);
     pinMode(LED_PWM, OUTPUT); 
 
     digitalWrite(LED_RED, HIGH);  //LED OFF
     digitalWrite(LED_GRN, HIGH);  //LED OFF
     digitalWrite(LED_YEL, HIGH);  //LED OFF
     digitalWrite(LED_PWM, HIGH);  //LED OFF
  

    //osc message buffer clear
     osc.flush();
     
}

void loop() {

    //osc arrive check
    if ( osc.available() ) {
      
//logMessage(&recMes);


          //toplevel address matching
          if( !strcmp( recMes.getAddress(0) , topAddress ) ){
                            
              //second level address matching           
                if( !strcmp( recMes.getAddress(1) , subAddress[0] ) ){
                           
                      red();
                
                }
                
                else if( !strcmp( recMes.getAddress(1) , subAddress[1] ) ){
                           
                      green();
                
                }
                else if( !strcmp( recMes.getAddress(1) , subAddress[2] ) ){
                           
                      yellow();
                
                }
                
                else if( !strcmp( recMes.getAddress(1) , subAddress[3] ) ){
                           
                      pwm();
                
                }
                
  
          }
 
    }
    

}

void red(){

   if(recMes.getArgInt(0) == 1){  
          digitalWrite(LED_RED, LOW);  //LED_ON
   }
   else{
          digitalWrite(LED_RED, HIGH);  //LED OFF
   }
  
}

void green(){

   if(recMes.getArgInt(0) == 1){  
          digitalWrite(LED_GRN, LOW);  //LED_ON
   }
   else{
          digitalWrite(LED_GRN, HIGH);  //LED OFF
   }
  
}

void yellow(){

   if(recMes.getArgInt(0) == 1){  
          digitalWrite(LED_YEL, LOW);  //LED_ON
   }
   else{
          digitalWrite(LED_YEL, HIGH);  //LED OFF
   }
  
}

void pwm(){
  byte value=(byte)recMes.getArgInt(0);
  analogWrite(LED_PWM, 255-value);
  
}



// *********  utility  ***********************************


void logMessage(OSCMessage *mes){
  
    uint8_t *ip=mes->getIp();
  
     //disp ip & port
    Serial.print("from IP:");
    Serial.print(ip[0],DEC);
    Serial.print(".");
    Serial.print(ip[1],DEC);
    Serial.print(".");
    Serial.print(ip[2],DEC);
    Serial.print(".");
    Serial.print(ip[3],DEC);
    Serial.print(" port:");
    Serial.print(mes->getPort(),DEC);
    Serial.print("   ");
    
    //disp adr
    for(int i = 0 ; i < mes->getAddressNum() ; i++){
      
      Serial.print(mes->getAddress(i));
      
    }
    
    
    //disp type tags
    Serial.print("  ,");
    for(int i = 0 ; i < mes->getArgNum() ; i++){
      
      Serial.print(mes->getTypeTag(i));
      
    }
    Serial.print(" ");
   
   
   //disp args
    for(int i = 0 ; i < mes->getArgNum() ; i++){
      
      switch( mes->getTypeTag(i) ){
        
        case 'i': {
                      
                      Serial.print( mes->getArgInt(i) );
                  }
          break;
        
        case 'f':  {
                      
                      Serial.print( mes->getArgFloat(i) );
                  }
          break;
         
      }
      
       Serial.print(" ");
      
    }
    
    Serial.println(""); 
    
}

